﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace WellDone
{
    public partial class EditMenuOptionDialog : ChildWindow
    {
        public ServiceReferenceOpenRPOS.MenuOption mo;
        List<int> menuItemIDs = new List<int>();
        public int menuitem = -1;
        public EditMenuOptionDialog()
        {
            InitializeComponent();
        }

        public void init()
        {
            // Load menu categories
            cmbMenuCategory.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.MenuCategory mc in Globals.menuItems.menuItemMenuCategoryRecords)
            {
                cmbMenuCategory.Items.Add(mc.col_Name);
            }
            if (cmbMenuCategory.Items.Count() > 0)
                cmbMenuCategory.SelectedIndex = 0;
        }

        private void cmbMenuCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Clear out the menu item combo box
            cmbMenuItem.Items.Clear();
            menuItemIDs.Clear();
            if (cmbMenuCategory.SelectedIndex >= 0)
            {
                // Load menu items
                foreach (ServiceReferenceOpenRPOS.MenuItem mi in Globals.db.MenuItemRecords)
                    if (mi.col_MenuCategoryID == Globals.menuItems.menuItemMenuCategoryRecords[cmbMenuCategory.SelectedIndex].col_ID)
                    {
                        cmbMenuItem.Items.Add(mi.col_FullName);
                        menuItemIDs.Add(mi.col_ID);
                    }
                if (cmbMenuItem.Items.Count > 0)
                    cmbMenuItem.SelectedIndex = 0;
            }
        }


        //Edit Menu Option
        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Ensure an item has been selected
            if (cmbMenuCategory.SelectedIndex >= 0 && cmbMenuItem.SelectedIndex >= 0)
            {
                if (Globals.menuItems.cmbOptionGroups.SelectedIndex >= 0)
                {
                    // Ensure that the selected menu item/option group has not been already added
                    int opt_grp_id = Globals.db.OptionGroupRecords[Globals.menuItems.cmbOptionGroups.SelectedIndex].col_ID;
                    int menu_item_id = menuItemIDs[cmbMenuItem.SelectedIndex];
                    await Globals.db.GetMenuOptionObjects("MenuItemID=" + menuitem.ToString() + " and OptionGroupID=" + opt_grp_id.ToString());
                    if (Globals.db.MenuOptionRecords.Count > 0)
                    {
                        Globals.db.MenuOptionRecords[0].col_MenuItemID = menu_item_id;
                        Globals.db.MenuOptionRecords[0].bFilterMenuItemID = true;
                        Globals.db.MenuOptionRecords[0].col_OptionGroupID = opt_grp_id;
                        Globals.db.MenuOptionRecords[0].bFilterOptionGroupID = true;
                        Globals.db.MenuOptionRecords[0].col_Checked = (cbchecked.IsChecked == true);
                        Globals.db.MenuOptionRecords[0].bFilterChecked = true;
                        await Globals.db.UpdateMenuOptionObject(Globals.db.MenuOptionRecords[0]);
                        Globals.menuItems.initMenuOptions();
                        this.DialogResult = true;
                    }
                }
             }  
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

